import sys
from PyQt5.QtWidgets import (
    QApplication, QWidget, QHBoxLayout, QVBoxLayout,
    QPushButton, QLabel, QFrame, QSizePolicy, QTextEdit
)
from PyQt5.QtCore import Qt, QPropertyAnimation, QEasingCurve
from PyQt5.QtGui import QPixmap, QFont


class Ui_Form:
    def setupUi(self, Form):
        Form.resize(1200, 700)  # 增加窗口大小以容纳结果显示区域
        Form.setStyleSheet("""
            QWidget {
                background-color: #f5f6fa;
                font-family: "Segoe UI", sans-serif;
                font-size: 14px;
            }
            QPushButton {
                background-color: #3f51b5;
                color: white;
                border-radius: 8px;
                padding: 10px;
                font-size: 16px;
                min-height: 40px;
            }
            QPushButton:hover {
                background-color: #303f9f;
            }
            QLabel {
                background-color: #ffffff;
                border-radius: 10px;
                border: 2px solid #dcdde1;
                padding: 5px;
            }
            QFrame#separator {
                background-color: #dcdde1;
            }
            #sidebar, #btnContainer {
                background-color: #2f3640;
            }
            QTextEdit {
                background-color: #ffffff;
                border-radius: 10px;
                border: 2px solid #dcdde1;
                padding: 10px;
                font-size: 14px;
            }
            .result-label {
                font-weight: bold;
                font-size: 16px;
                color: #2f3640;
                background-color: #e1f5fe;
                border-radius: 8px;
                padding: 8px;
                border: 1px solid #b3e5fc;
            }
            .count-display {
                font-size: 24px;
                font-weight: bold;
                color: #3f51b5;
                background-color: rgba(255, 255, 255, 0.7);
                border-radius: 5px;
                padding: 2px 8px;
            }
        """)

        # 主窗口布局
        self.main_layout = QHBoxLayout(Form)
        self.main_layout.setSpacing(0)
        self.main_layout.setContentsMargins(0, 0, 0, 0)

        # ====== 左侧侧边栏（可滑动）======
        self.sidebar = QWidget()
        self.sidebar.setObjectName("sidebar")
        self.sidebar.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding)
        self.sidebar.setMinimumWidth(40)
        self.sidebar.setMaximumWidth(200)

        sidebar_layout = QVBoxLayout(self.sidebar)
        sidebar_layout.setSpacing(10)
        sidebar_layout.setContentsMargins(5, 20, 5, 20)

        self.btn_toggle_sidebar = QPushButton("⬅️ 收起")
        self.btn_toggle_sidebar.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
        
        # 创建按钮容器
        self.btn_container = QWidget()
        self.btn_container.setObjectName("btnContainer")
        btn_container_layout = QVBoxLayout(self.btn_container)
        btn_container_layout.setContentsMargins(0, 0, 0, 0)
        btn_container_layout.setSpacing(10)
        
        self.btn_open = QPushButton("📁 打开图像")
        self.btn_process = QPushButton("🔍 YOLO检测")
        self.btn_count = QPushButton("📊 计数结果")
        
        btn_container_layout.addWidget(self.btn_open)
        btn_container_layout.addWidget(self.btn_process)
        btn_container_layout.addWidget(self.btn_count)
        
        sidebar_layout.addWidget(self.btn_toggle_sidebar)
        sidebar_layout.addWidget(self.btn_container)
        sidebar_layout.addStretch()

        # ====== 分割线 ======
        self.separator = QFrame()
        self.separator.setObjectName("separator")
        self.separator.setFixedWidth(4)

        # ====== 右侧内容区域 ======
        content_widget = QWidget()
        content_layout = QVBoxLayout(content_widget)

        # 图像显示区域
        image_layout = QHBoxLayout()
        image_layout.setSpacing(20)
        image_layout.setContentsMargins(20, 20, 20, 10)  # 调整边距

        # 原始图像区域
        original_frame = QVBoxLayout()
        original_frame.setSpacing(10)
        self.label_original_title = QLabel("原始图像")
        self.label_original_title.setAlignment(Qt.AlignCenter)
        self.label_original_title.setStyleSheet("font-weight: bold; font-size: 16px; color: #3f51b5;")
        
        self.label_original = QLabel()
        self.label_original.setAlignment(Qt.AlignCenter)
        self.label_original.setMinimumSize(400, 300)
        
        # 添加示例图像
        self.label_original.setText("请加载图像")
        self.label_original.setStyleSheet("font-style: italic; color: #7f8c8d;")
        
        original_frame.addWidget(self.label_original_title)
        original_frame.addWidget(self.label_original)

        # 处理后图像区域
        processed_frame = QVBoxLayout()
        processed_frame.setSpacing(10)
        self.label_processed_title = QLabel("检测结果")
        self.label_processed_title.setAlignment(Qt.AlignCenter)
        self.label_processed_title.setStyleSheet("font-weight: bold; font-size: 16px; color: #3f51b5;")
        
        self.label_processed = QLabel()
        self.label_processed.setAlignment(Qt.AlignCenter)
        self.label_processed.setMinimumSize(400, 300)
        
        # 添加示例计数显示
        self.count_display = QLabel("检测目标: 0")
        self.count_display.setAlignment(Qt.AlignCenter)
        self.count_display.setStyleSheet(".count-display")
        self.count_display.setFixedHeight(40)
        
        processed_frame.addWidget(self.label_processed_title)
        processed_frame.addWidget(self.label_processed)
        processed_frame.addWidget(self.count_display)

        image_layout.addLayout(original_frame, stretch=1)
        image_layout.addLayout(processed_frame, stretch=1)

        content_layout.addLayout(image_layout, stretch=2)  # 图像区域占2份空间

        # ====== 结果显示区域 ======
        result_layout = QVBoxLayout()
        result_layout.setContentsMargins(20, 10, 20, 20)
        result_layout.setSpacing(10)
        
        result_title = QLabel("检测结果统计")
        result_title.setStyleSheet("font-weight: bold; font-size: 18px; color: #2f3640;")
        result_title.setAlignment(Qt.AlignCenter)
        
        # 结果统计信息
        stats_layout = QHBoxLayout()
        stats_layout.setSpacing(15)
        
        
        # 详细结果文本框
        self.result_text = QTextEdit()
        self.result_text.setReadOnly(True)
        self.result_text.setPlaceholderText("检测结果将显示在这里...")
        self.result_text.setFont(QFont("Consolas", 10))
        
        # 添加示例结果
        self.result_text.setPlainText(
            "=== 检测结果统计 ===\n"
            "总检测目标数: 0\n"
            "人物: 0\n"
            "车辆: 0 (轿车: 0, 卡车: 0)\n"
            "动物: 0 (狗: 0, 猫: 0)\n\n"
            "=== 详细检测结果 ===\n"
            "等待检测结果..."
        )
        
        result_layout.addWidget(result_title)
        result_layout.addLayout(stats_layout)
        result_layout.addWidget(self.result_text, stretch=1)
        
        content_layout.addLayout(result_layout, stretch=1)  # 结果区域占1份空间

        # 将组件添加到主布局中
        self.main_layout.addWidget(self.sidebar)
        self.main_layout.addWidget(self.separator)
        self.main_layout.addWidget(content_widget)

        # ===== 动画初始化 =====
        self.sidebar_animation = QPropertyAnimation(self.sidebar, b"minimumWidth")
        self.sidebar_animation.setDuration(300)
        self.sidebar_animation.setEasingCurve(QEasingCurve.InOutQuart)
        
        self.sidebar_animation2 = QPropertyAnimation(self.sidebar, b"maximumWidth")
        self.sidebar_animation2.setDuration(300)
        self.sidebar_animation2.setEasingCurve(QEasingCurve.InOutQuart)

        self.is_sidebar_expanded = True  # 初始状态为展开

    def toggle_sidebar(self):
        if self.is_sidebar_expanded:
            # 收起侧边栏，但保留40px宽度确保按钮可见
            self.sidebar_animation.setStartValue(200)
            self.sidebar_animation.setEndValue(40)
            self.sidebar_animation2.setStartValue(200)
            self.sidebar_animation2.setEndValue(40)
            self.btn_toggle_sidebar.setText("→")  # 只显示箭头
            # 隐藏其他按钮
            self.btn_container.hide()
        else:
            # 展开侧边栏
            self.sidebar_animation.setStartValue(40)
            self.sidebar_animation.setEndValue(200)
            self.sidebar_animation2.setStartValue(40)
            self.sidebar_animation2.setEndValue(200)
            self.btn_toggle_sidebar.setText("⬅️ 收起")
            # 显示其他按钮
            self.btn_container.show()

        self.is_sidebar_expanded = not self.is_sidebar_expanded
        self.sidebar_animation.start()
        self.sidebar_animation2.start()


class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.setWindowTitle("YOLO图像检测与计数系统")

        # 连接按钮事件（仅用于界面展示，无实际功能）
        self.ui.btn_toggle_sidebar.clicked.connect(self.ui.toggle_sidebar)
        self.ui.btn_open.clicked.connect(self.demo_open_image)
        self.ui.btn_process.clicked.connect(self.demo_process_image)
        self.ui.btn_count.clicked.connect(self.demo_show_count_results)

    def demo_open_image(self):
        # 示例：模拟图像加载
        self.ui.label_original.setText("已加载示例图像")
        self.ui.label_original.setStyleSheet("")
        self.ui.label_processed.setText("等待检测结果...")
        self.ui.count_display.setText("检测目标: 0")
        
    def demo_process_image(self):
        # 示例：模拟检测过程
        self.ui.label_processed.setText("检测结果图像")
        self.ui.count_display.setText("检测目标: 12")
        
        # 更新统计卡片
        self.update_stat_card(self.ui.card_total, "总检测目标", "12")
        self.update_stat_card(self.ui.card_person, "人物", "5")
        self.update_stat_card(self.ui.card_vehicle, "车辆", "4")
        self.update_stat_card(self.ui.card_animal, "动物", "3")
        
        # 更新结果文本框
        self.ui.result_text.setPlainText(
            "=== 检测结果统计 ===\n"
            "总检测目标数: 12\n"
            "人物: 5\n"
            "车辆: 4 (轿车: 3, 卡车: 1)\n"
            "动物: 3 (狗: 2, 猫: 1)\n\n"
            "=== 详细检测结果 ===\n"
            "目标 1: person (置信度: 98.5%)\n"
            "位置: x=0.352, y=0.421, 宽=0.085, 高=0.215\n"
            "--------------------------------------------------\n"
            "目标 2: car (置信度: 95.2%)\n"
            "位置: x=0.621, y=0.532, 宽=0.142, 高=0.098\n"
            "--------------------------------------------------\n"
            "目标 3: dog (置信度: 89.7%)\n"
            "位置: x=0.123, y=0.754, 宽=0.065, 高=0.112\n"
            "--------------------------------------------------\n"
            "... 更多结果 ..."
        )
    
    def update_stat_card(self, card, title, count):
        # 更新统计卡片内容
        layout = card.layout()
        layout.itemAt(0).widget().setText(title)
        layout.itemAt(1).widget().setText(count)
        
    def demo_show_count_results(self):
        # 示例：显示计数结果
        self.ui.result_text.setPlainText(
            "=== 计数结果汇总 ===\n\n"
            "总检测目标数: 12\n\n"
            "--- 按类别统计 ---\n"
            "人物: 5\n"
            "  - 成人: 3\n"
            "  - 儿童: 2\n\n"
            "车辆: 4\n"
            "  - 轿车: 3\n"
            "  - 卡车: 1\n\n"
            "动物: 3\n"
            "  - 狗: 2\n"
            "  - 猫: 1\n\n"
            "检测时间: 2023-06-15 14:30:22\n"
            "处理耗时: 0.45秒"
        )


# ========== 程序入口 ==========
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())